<template>
  <div class="container">
    <div class="app-container">
      <!-- 树形结构 -->
      <el-tree :data="depts" :props="defaultProps" default-expand-all :expand-on-click-node="false">
        <!-- layout布局 -->
        <!-- v-slot="{Node,data}" 使用自定义插槽会传入当前节点对象和当前节点数据 -->
        <template v-slot="{data}">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4"><span class="tree-manager">{{ data.managerName }}</span>
              <el-dropdown @command="handleEdit($event,data.id)">
                <!-- 显示区域 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉行 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <add-dept ref="AddDept" :show-dialog.sync="showDialog" :father-id="fatherId" @updateDepartment="getDepartmentList" />
  </div>
</template>

<script>
import { deleteDepartment, getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      depts: [{
        name: '传智教育',
        children: [{
          name: '总裁办'
        }, {
          name: '董事会'
        }, {
          name: '财务部'
        }]
      }],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      fatherId: null
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartment()
      this.depts = transListToTreeData(res, 0)
    },
    handleEdit(command, id) {
      if (command === 'add') {
        this.showDialog = true
        this.fatherId = id
      } else if (command === 'edit') {
        this.showDialog = true
        this.fatherId = id
        // 父传子  子更新数据 是异步操作 需要等待数据渲染完再往下跳
        this.$nextTick(() => {
          this.$refs.AddDept.showDetail()
        })
      } else {
        this.$confirm('您确定要删除该部门吗', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
          .then(async() => {
            await deleteDepartment(id)
            this.$message.success('删除成功')
            this.getDepartmentList()
          })
          .catch(action => {
            this.$message({
              type: 'info',
              message: action === 'cancel'
                ? '放弃保存并离开页面'
                : '停留在当前页面'
            })
          })
      }
    }
  }
}
</script>

<style>
.app-container{
   padding:30px 140px;
   font-size:14px;
}
.tree-manager{
    width:100px;
    display:inline-block;
    margin:10px
  }

</style>
